﻿@{
    Layout = null;
}

<h2>RoleManage</h2>
<link href="~/Content/css/bootstrap.min.css" rel="stylesheet" />
<script src="~/Scripts/js/jquery.min.js"></script>
<script src="~/Scripts/js/bootstrap.min.js"></script>

<input id="txtDeptName" type="text" />
<input type="button" value="搜索" onclick="LoadDeptList()" />
<input type="button" value="添加部门" onclick="funAddDept()" />
<div style="clear:both">
    <table id="t1" class="table-bordered table table-condensed table-responsive" style="width:300px;float:left">
        <thead>
            <tr>
                <th>部门编号</th>
                <th>部门名称</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
    <table id="t2" class="table-bordered table table-condensed table-responsive" style="float:left;width:500px;">
        <thead>
            
        </thead>
        <tbody></tbody>
    </table>
</div>
<script>
    setTimeout(LoadDeptList, 500);
    
    //加载部门列表
    function LoadDeptList() {
        $.ajax({
            type: "post",
            url: "/Employee/RoleList",
            data: { DeptName: $("#txtDeptName").val() },
            success: function (obj) {
                if (obj != "null") {
                    obj = eval("" + obj + "");
                    var str = "";
                    for (var i = 0; i < obj.length; i++) {
                        str += "<tr><td>" + obj[i].DeptID + "</td><td>" + obj[i].DeptName + "</td><td><a href='#' onclick='funEdit(" + obj[i].DeptID + ",\"" + obj[i].DeptName + "\")'>查看</a></td></tr>";
                    }
                    $("#t1 tbody").html(str);
                }
                else{
                    $("#t1 tbody").empty();
                }
            }
        })
    }

    //添加部门
    function funAddDept() {
        var DeptName = $("#txtDeptName").val();
        if (DeptName != "") {
            $.ajax({
                type: "post",
                url: "/Employee/InsertDeptInfo",
                data: { DeptName: DeptName },
                success: function (obj) {
                    if (obj > 0) {
                        $("#txtDeptName").val("");
                        LoadDeptList();
                    }
                    else
                        alert("添加失败！");
                }
            })
        }
        else
            alert("请输入要添加的部门");
    }

    //加载此部门下的员工
    function funEdit(DeptID,DeptName) {
        $.ajax({
            type: "post",
            url: "/Employee/GetEmployeeByDeptID/" + DeptID,
            success: function (obj) {
                if (obj != "[]") {
                    obj = eval("" + obj + "");

                    var head = "<tr><th>编号</th><th>账户</th><th>年龄</th><th>学位</th><th>头像</th><th>部门</th></tr>"
                    var str = "";
                    for (var i = 0; i < obj.length; i++) {
                        str += "<tr><td>" + obj[i].EmployID + "</td><td>" + obj[i].Account + "</td><td>" + obj[i].Age + "</td><td>" + obj[i].Degree + "</td><td><img src='" + obj[i].EmployeePhoto + "' style='width:80px;height:50px' /></td><td><B>" + DeptName + "</B></td></tr>"
                    }
                    $("#t2 thead").html(head);
                    $("#t2 tbody").html(str);
                }
                else {
                    $("#t2 thead").empty();
                    $("#t2 tbody").empty();
                }
            }
        })
    }
</script>